package com.zsk.shop.task;

import com.zsk.shop.common.constant.RedisKeys;
import com.zsk.shop.common.utils.DateUtils;
import com.zsk.shop.common.utils.RedisUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.ShopGoods;
import com.zsk.shop.entity.ShopOrder;
import com.zsk.shop.enums.EnumDataDictionaryKey;
import com.zsk.shop.enums.EnumOrderStatus;
import com.zsk.shop.service.ShopGoodsService;
import com.zsk.shop.service.ShopOrderPointsToAccountService;
import com.zsk.shop.service.ShopOrderService;
import com.zsk.shop.service.yulink.LinkShopLogInfoService;
import com.zsk.shop.service.yulink.YuLinkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 定时任务
 *
 * @author Ys
 * @version 1.0
 * @date 2022/2/24 15:46
 */
@Component
@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class FixedPrintTask {

    private static final Logger logger = LoggerFactory.getLogger(FixedPrintTask.class);
    @Resource
    private ShopOrderService orderService;
    @Resource
    private ShopGoodsService shopGoodsService;
    @Resource
    private ShopOrderPointsToAccountService shopOrderPointsToAccountService;
    @Resource
    private YuLinkService yuLinkService;
    @Resource
    private LinkShopLogInfoService linkShopLogInfoService;

    /**
     * 每分钟查询且修改商品定时发布
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void handleGoodsStatus() {
        logger.info("===================开始处理查询且修改商品定时发布===================");
        List<ShopGoods> wmGoodsList = shopGoodsService.listUnPublishGoods();
        if (ToolUtil.isEmpty(wmGoodsList)) {
            return;
        }
        for (ShopGoods shopGoods : wmGoodsList) {
            shopGoods.setStatus(true);
            shopGoods.setUpdateTime(new Date());
        }
        shopGoodsService.saveOrUpdateBatch(wmGoodsList);
        //修改商品活动中的商品内容
        List<String> goodsNos = wmGoodsList.stream().map(ShopGoods::getGoodsNo).collect(Collectors.toList());
    }

    /**
     * 处理未支付订单是否超时取消
     */
    @Scheduled(cron = "*/30 * * * * ?")
    public void updateOrder() {
        logger.info("===================开始处理未支付订单是否超时取消===================");
        List<ShopOrder> orders = orderService.getBaseMapper().getList(EnumOrderStatus.PAY_NOT.getCode());
        for (ShopOrder order : orders) {
            Integer cancelTime = Integer.parseInt(RedisUtil.get(RedisKeys.dictionaryKey(EnumDataDictionaryKey.ORDER_CANCEL_TIME.getKey())).toString());
            Date endTime = DateUtils.addMinuteOfDate(order.getCreateTime(), cancelTime);
            if (System.currentTimeMillis() >= endTime.getTime()) {
                orderService.updateOrderEndStatus(order.getOrderNo(), EnumOrderStatus.ORDER_CANCEL_TIME.getCode());
            }
        }
    }

    /**
     * 每分钟查询未收货订单，发货14天后自动转换成完成订单
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void handleDeliveryAddressStatus() {
        logger.info("===================开始处理查询未收货订单，发货14天后自动转换成完成订单===================");
        orderService.handleDeliveryOvertimeStatus();
    }

    @Scheduled(cron = "0 1 0 * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void link() {
        logger.info("===================12点01分开始处理昨日用户下级是否满足释放要求数据===================");
        linkShopLogInfoService.handleLinkShopLogInfoSubUser(DateUtils.addDayOfDate(new Date(), -1));
        logger.info("===================" + DateUtils.format(new Date()) + "开始处理昨日用户登录数据至待解冻列表===================");
        yuLinkService.handleLoginLinkPointDetail(DateUtils.addDayOfDate(new Date(), -1));
        logger.info("===================" + DateUtils.format(new Date()) + "开始处理解冻金额至账户余额===================");
        shopOrderPointsToAccountService.task(DateUtils.addDayOfDate(new Date(), -1));
    }

}
